package my.study.org.mongodb.crud.impl;

import java.util.List;

import my.study.org.mongodb.bo.KeyWord;
import my.study.org.mongodb.constant.MongoDBConstant;
import my.study.org.mongodb.crud.KeyWordDAOInter;
import my.study.org.mongodb.factory.impl.MongoFactoryImpl;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

public class KeyWordDAOImpl implements KeyWordDAOInter {
	private DBCollection coll;
	
	public KeyWordDAOImpl() {
		coll = MongoFactoryImpl.getMongoFactory().getMongoDB(MongoDBConstant.MONGO_DB_NAME).getCollection("keyword_coll");
	}
	
	public boolean save(KeyWord keyWord) {
		DBObject doc = new BasicDBObject();
		doc.put("plan_id", keyWord.getPlanID());
		doc.put("unit_id", keyWord.getUnitID());
		doc.put("value", keyWord.getValue());
		doc.put("word_id", keyWord.getWordID());
		doc.put("word_info_id", keyWord.getWordInfoID());
		coll.save(doc);
		return true;
	}

	public boolean save(List<KeyWord> keyWords) {
		
		DBObject[] docs = new DBObject[keyWords.size()];
		for (int i = 0; i < keyWords.size(); i++) {
			DBObject doc = object2DBObject(keyWords.get(i));
			docs[i] = doc;
		}
		coll.insert(docs);
		return true;
	}
	
	private DBObject object2DBObject(KeyWord keyWord) {
		DBObject doc = new BasicDBObject();
		doc.put("plan_id", keyWord.getPlanID());
		doc.put("unit_id", keyWord.getUnitID());
		doc.put("value", keyWord.getValue());
		doc.put("word_id", keyWord.getWordID());
		doc.put("word_info_id", keyWord.getWordInfoID());
		coll.save(doc);
		return doc;
	}

	public boolean clear() {
		this.coll.drop();
		return true;
	}

	public long count() {
		return this.coll.count();
	}
}
